#!/bin/bash
source ~/command/common

if __windows;then
    ips=($(node -e " \
    const os = require('os'); \
    const interfaces = os.networkInterfaces(); \
    let ips=[]; \
    for (let devName in interfaces) { \
        var iface = interfaces[devName]; \
        for (var i = 0; i < iface.length; i++) { \
            var alias = iface[i]; \
            if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) { \
                ips.push(alias.address); \
            } \
        } \
    } \
    console.log(ips.join(' '))"))
else
    ips=(`ifconfig -a | grep -w inet | grep -v '127.0.0.1' | awk '{ print $2}'`)
fi

function myping() {
    ping -c2 -i0.3 -W1 $1 &>/dev/null
    if [ $? -eq 0 ];then
        echo "$1 is up"
    fi
}

if [ "$1" = "-h" ];then
    echo "xconfig -h|-i:show ip|-ips: show ips connect|-r:reset|-android:xx|default: modify config"
elif [ "$1" = "-i" -o "$1" = "-ip" ];then
    echo ${ips[@]}
else
    len=${#ips[@]}
    if [ $len -gt 1 ];then
        _list_=(${ips[@]})
        __radiobox "ip=$_TARGET_" "once" "work" "select bellow ips"
        if [ -z "$ip" ];then
            ip=${ips[0]}
        fi
    else
        ip=${ips[0]}
    fi
    if [ "$1" = "-ips" ];then
        pre=${ip%.*}
        for i in {1..254};do
            myping $pre.$i &
        done
    elif [ "$1" = "-android" ];then
        echo "/usr/local/opt/android-sdk"
        echo "/Users/whxx/Library/Android/sdk"
        echo "C:\\\\Users\\\\Admin\\\\AppData\\\\Local\\\\Android\\\\Sdk"
    elif [ "$1" = "-r" ];then
        vim -e -s config.js -c ":%s/\(apiServer:.*\)\/\d\+\.\d\+\.\d\+\.\d\+:/\1\/localhost:/" -c ":wq"
        echo "set localhost done"
    else
        if ! [ -a config.js ];then
            echo "have no config.js"
            exit
        fi
        vim -e -s config.js -c ":%s/\(apiServer:.*:\/\/\)\(\d\+\.\d\+\.\d\+\.\d\+:\)\|\(localhost:\)/\1${ip}:/" -c ":wq"
        echo "set $ip done"
    fi
fi
